package com.cai.dao.impl;

import com.cai.dao.GoodsDao;
import com.cai.entity.ItemType;
import com.cai.entity.Items;
import com.cai.util.C3p0Util;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author RuoLi
 */
public class GoodsDaoImpl implements GoodsDao {
    static QueryRunner runner = new QueryRunner(C3p0Util.getDataSource());
    List<Items> itemsList = new ArrayList<>();
    List<ItemType> itemTypeList = new ArrayList<>();

    @Override
    public List<Items> selectGoodsList() {
        String sql = "select * from items";
        try {
            itemsList = runner.query(sql, new BeanListHandler<>(Items.class));
            if (itemsList != null && !itemsList.isEmpty()) {
                return itemsList;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int insertGoods(Items item) {
        String sql = "INSERT INTO items(item_name,item_num,item_type,item_log) VALUES (?,?,?,?)";
        Object[] args = {item.getItem_name(), item.getItem_num(), item.getItem_type(), item.getItem_log()};
        try {
            int status = runner.update(sql, args);
            if (status != 0) {
                return 1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public int deleteGoods(int id) {
        String sql = "DELETE FROM items WHERE item_id = " + id;
        try {
            int status = runner.update(sql);
            if (status != 0) {
                return 1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public int selectTargetGoods(String itemName) {
        String sql = "select count(*) from items where item_name=?";
        int count = UtilsDaoImpl.selectCount(sql, new String[]{itemName});
        if (count != 0) {
            return 1;
        }
        return -1;
    }

    @Override
    public int updateGoods(Items item) {
        String sql = " UPDATE items SET item_num = ?,item_type = ?,item_log=? WHERE item_name = ?";
        Object[] params = {item.getItem_num(), item.getItem_type(), item.getItem_log(), item.getItem_name()};
        try {
            int count = runner.update(sql, params);
            if (count != 0) {
                return 1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public List<ItemType> selectItemType() {
        String sql = "select * from item_type";
        try {
            itemTypeList = runner.query(sql, new BeanListHandler<>(ItemType.class));
            if (itemTypeList != null) {
                return itemTypeList;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int insertItemType(String itemTypeName) {
        String sql = "INSERT INTO item_type (itemType_name) VALUES (?)";
        return deleteInsertItemType(itemTypeName, sql);
    }

    @Override
    public int deleteItemTypeName(String itemTypeName) {
        String sql = "DELETE FROM item_type WHERE itemType_name = ?";
        return deleteInsertItemType(itemTypeName, sql);
    }

    @Override
    public int sendItem(int sendItemNum, String itemName) {
        String sql = "UPDATE items SET item_num = item_num - ?, item_send = ? WHERE item_name = ?";
        Object[] args = {sendItemNum, sendItemNum, itemName};
        return sendReturnItem(sql, args, sendItemNum, itemName);
    }

    @Override
    public int returnItem(int returnItemNum, String itemName) {
        String sql = "UPDATE items SET item_num = item_num + ?, item_send = item_send-?, item_return= ? WHERE item_name = ?";
        Object[] args = {returnItemNum, returnItemNum, returnItemNum, itemName};
        return sendReturnItem(sql, args, returnItemNum, itemName);
    }

    private int deleteInsertItemType(String itemTypeName, String sql) {
        Object[] params = {itemTypeName};
        try {
            int count = runner.update(sql, params);
            if (count != 0) {
                return 1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    private int sendReturnItem(String sql, Object[] params, int ItemNum, String itemName) {
        try {
            int count = runner.update(sql, params);
            if (count != 0) {
                return 1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

}
